/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.cli;

import java.util.Iterator;

/**
 * Arguments that denote a target.
 * These arguments don't have a fixed name, but capture unhandled arguments
 * @author Giulio Franco <giulio_franco@msn.com>
 *
 * @param <ResultType>	Type of result processed
 */
public abstract class CLITargetArgument<ResultType> extends CLIArgument<ResultType> {
	/**
	 * Enumerator for common errors
	 */
	public static enum Errors {
		DUPLICATE_TARGET("cliarg.error.target.duplicate");
		
		public final String errorFormat;
		private Errors(String errorFormat) {
			this.errorFormat = errorFormat;
		}
	}
	
	/**
	 * Creates a new CLITargetArgument
	 * @param description	Argument description
	 * @param required		Boolean indicating if the argument is required
	 * @param unique		Boolean indicating if the argument is unique
	 */
	public CLITargetArgument(String description, boolean required, boolean unique) {
		super("", description, required, unique);
	}

	@Override
	public boolean inserted(String matcher, Iterator<String> args,
			ParseResult<? extends ResultType> result) {
		
		if (isUnique() && result.getArguments().contains(this)) {
			result.addError(new ParseError<ResultType>(
					this, Errors.DUPLICATE_TARGET.errorFormat));
			return false;
		} else {
			return super.inserted(matcher, args, result)
					&& argumentInserted(matcher, args, result);
		}
	}
	
	@Override
	public boolean isOk(ParseResult<? extends ResultType> result) {
		if (super.isOk(result)) {
			if (result.getArguments().contains(this)) {
				return true;
			} else {
				return argumentNotInserted(result);
			}
		} else {
			return false;
		}
	}

	/**
	 * Called upon insertion of the argument
	 * @param matcher	Matched string (content of the argument)
	 * @param args		Iterator pointing to the next argument
	 * @param result	Result object
	 * @return	True iff the parse can continue
	 */
	protected abstract boolean argumentInserted(String matcher,
			Iterator<String> args, ParseResult<? extends ResultType> result);
	
	/**
	 * Called at the end of the parse, if the argument was not provided
	 * @param result	Result object
	 * @return	True iff the parse can continue
	 */
	protected abstract boolean argumentNotInserted(
			ParseResult<? extends ResultType> result);
}
